/*
 */

package com.jams.goodox.utils;

import com.jams.goodox.TownApplicationLike;
import com.jams.goodox.entity.User;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jams on 2016/6/22.
 * ***
 * Tel: ***
 */

public class DataCacheManager {
    private static final String USER_INFO_NAME = "/userInfo.se";
    private static final String UNITINFO_INFO_NAME = "/unitInfo.se";
    private static final String SEARCH_HISTORY = "/searchHistory.se";
    private static final String JOB_TAG = "/jobTag.se";
    private static DataCacheManager mInstance;
    private User userInfo;
    private List<String> searchHistory;
    private List<String> jobTag;

    // static
    public static DataCacheManager getInstance() {
        synchronized (DataCacheManager.class) {
            if (mInstance == null) {
                mInstance = new DataCacheManager();
            }
        }

        return mInstance;
    }

    // public
    public User getUser() {
        return userInfo == null ? new User() : userInfo;
    }

    // private
    private DataCacheManager() {
        userInfo = null;
        searchHistory = new ArrayList<String>();
        jobTag = new ArrayList<>();
        readUserInfo();
        readUnitInfo();
        readSearchHistory();
        readJobTag();
    }

    public void setUser(User user) {
        deleteAllUserData();
        userInfo = user;
        saveUserInfo();
    }

    private void readUserInfo() {
        String userInfoFilePath = getUserInfoPath();
        userInfo = (User) readObject(userInfoFilePath);
    }

    private void readUnitInfo() {
        String unitInfoFilePath = getUnitInfoPath();
    }

    private void readSearchHistory() {
        String searchHistoryFilePath = getSearchHistoryPath();
        searchHistory = (List<String>) readObject(searchHistoryFilePath);
    }

    private void readJobTag() {
        String jobTagFilePath = getJobTagPath();
        jobTag = (List<String>) readObject(jobTagFilePath);
    }

    public void setJobTag(List<String> jobTags) {
        this.jobTag = jobTags;
        saveJobTag();
    }

    public List<String> getJobTag() {
        return jobTag == null ? new ArrayList<String>() : jobTag;
    }

    public void setSearchHistory(List<String> histories) {
        searchHistory = histories;
        saveSearchHistory();
    }

    public List<String> getSearchHistory() {
        return searchHistory;
    }

    //判断是否是自己
    public boolean isCurrentUser(String userId) {
        if (userInfo != null && userInfo.getUserId().equals(userId)) {
            return true;
        }
        return false;
    }

    private void saveUserInfo() {
        if (userInfo != null) {
            saveObject(getUserInfoPath(), userInfo);
        }
    }

    private void saveSearchHistory() {
        if (searchHistory != null) {
            saveObject(getSearchHistoryPath(), searchHistory);
        }
    }

    private void saveJobTag() {
        if (jobTag != null) {
            saveObject(getJobTagPath(), jobTag);
        }
    }

    private String getUserInfoPath() {
        String filePath = TownApplicationLike.getInstance().getFilesDir().getPath();
        filePath += DataCacheManager.USER_INFO_NAME;
        return filePath;
    }

    private String getUnitInfoPath() {
        String filePath = TownApplicationLike.getInstance().getFilesDir().getPath();
        filePath += DataCacheManager.UNITINFO_INFO_NAME;
        return filePath;
    }

    private String getSearchHistoryPath() {
        String filePath = TownApplicationLike.getInstance().getFilesDir().getPath();
        filePath += DataCacheManager.SEARCH_HISTORY;
        return filePath;
    }

    private String getJobTagPath() {
        String filePath = TownApplicationLike.getInstance().getFilesDir().getPath();
        filePath += DataCacheManager.JOB_TAG;
        return filePath;
    }

    /*
    * @function 从本地读取保存的对象
    * @author D-light
    * @return Object
    */
    private Object readObject(String filePath) {
        Object object = null;

        if (!Utils.isExists(filePath)) {
            return null;
        }

        FileInputStream fis = null;
        ObjectInputStream ois = null;

        File f = new File(filePath);
        try {
            fis = new FileInputStream(f);
            ois = new ObjectInputStream(fis);
            object = ois.readObject();
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return object;
    }

    /*
   * @function 保存对象到本地
   * @author D-light
   * @return Object
   */
    private void saveObject(String filePath, Object object) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        File f = new File(filePath);
        try {
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //用户配置信息放在内部存储，不需要手动删除
    public void deleteAllUserData() {
        File file = new File(getUserInfoPath());
        if (file.delete()) {
//            Log.i("dataCache", "删除用户配置信息");
        }
        userInfo = null;
    }

    public void deleteAllSearchHistoryData() {
        File file = new File(getSearchHistoryPath());
        if (file.delete()) {
//            Log.i("dataCache", "删除用户配置信息");
        }
        searchHistory = null;
    }
}
